package jmu.edu.yjc.controller;

import jmu.edu.yjc.domain.ResponseResult;
import jmu.edu.yjc.domain.dto.RoleDto;
import jmu.edu.yjc.domain.entity.Role;
import jmu.edu.yjc.service.RoleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;

@RestController
@RequestMapping("/system/role")
public class RoleController {
    @Resource
    private RoleService roleService;
    @GetMapping("/list")
    public ResponseResult listRole(String roleName, String status, Integer pageNum, Integer pageSize){
        return roleService.listRole(roleName,status,pageNum,pageSize);
    }
    @PutMapping("/changeStatus")
    public ResponseResult changeStatus(Long roleId, String status){
        Role role = new Role();
        role.setId(roleId);
        role.setStatus(status);
        roleService.updateById(role);
        return ResponseResult.okResult();
    }
    @PostMapping
    public ResponseResult addRole(@RequestBody RoleDto role){
        return roleService.addRole(role);
    }
    @GetMapping("/{roleId}")
    public ResponseResult editRole(@PathVariable("roleId") Long roleId){
        return ResponseResult.okResult(roleService.getById(roleId));
    }
    @PutMapping
    public ResponseResult doEditRole(@RequestBody RoleDto role)
    {
        roleService.updateRole(role);
        return ResponseResult.okResult();
    }

    @DeleteMapping("/{roleId}")
    public ResponseResult remove(@PathVariable(name = "roleId") String roleId) {
        //这里只进行逻辑删除，也就是说role_menu表不用跟着删
        String[] ids = roleId.split(",");
        roleService.removeByIds(Arrays.asList(ids));
        return ResponseResult.okResult();
    }
    @GetMapping("/listAllRole")
    public ResponseResult listAllRole(){
        return roleService.listAllRole();
    }
}
